package com.es.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.es.entiy.SearchParam;
import com.es.service.ITestService;
import org.apache.lucene.search.join.ScoreMode;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.NestedQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author SeniorTong丶
 * @Date 2021/11/24 11:35
 * @Version 1.0
 * @Description
 */
@Service
public class TestServiceImpl implements ITestService {

    @Autowired
    private RestHighLevelClient restHighLevelClient;

    /**
     * 联查 组合数据
     *
     * @param param
     * @return
     */
    @Override
    public List<Map<String, Object>> listAll(SearchParam param) {

        List<Map<String, Object>> listMap = new ArrayList<>();
        GetResponse documentFields = null;
        try {
            // 根据id查新单个
            GetRequest request = new GetRequest("gongdan_detail", "db6bc958-9016-5a7f-9c2d-1c0cd86f6817");
            documentFields = restHighLevelClient.get(request, RequestOptions.DEFAULT);


            //构建查询器
            SearchRequest request1 = new SearchRequest("chuku_detail");
            //设置查询条件
            SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
            //termQuery 精确查询
            sourceBuilder.query(QueryBuilders.termQuery("SO_ITEM_ID.keyword", "db6bc958-9016-5a7f-9c2d-1c0cd86f6817"));
            //将条件放入查询器
            request1.source(sourceBuilder);

            //请求获取数据
            SearchResponse search = restHighLevelClient.search(request1, RequestOptions.DEFAULT);
            SearchHit[] hits = search.getHits().getHits();
            for (SearchHit hit : hits) {
                Map<String, Object> map = new HashMap<>();
                map.put("ID", Long.valueOf(hit.getSourceAsMap().get("ID").toString()));
                map.put("DIM_SHOP_ID", hit.getSourceAsMap().get("DIM_SHOP_ID").toString());
                map.put("MEA_REL_WAVFROMID", hit.getSourceAsMap().get("MEA_REL_WAVFROMID").toString());
                map.put("FAD_REL_GOODSNO", hit.getSourceAsMap().get("FAD_REL_GOODSNO").toString());
                map.put("FAD_REL_GOODSNAME", hit.getSourceAsMap().get("FAD_REL_GOODSNAME").toString());
                map.put("FAC_REL_GOODSINPRICE", hit.getSourceAsMap().get("FAC_REL_GOODSINPRICE").toString());
                map.put("DIM_DT", hit.getSourceAsMap().get("DIM_DT").toString());
                map.put("BATCH_NO", hit.getSourceAsMap().get("BATCH_NO").toString());
                map.put("SO_ITEM_ID", hit.getSourceAsMap().get("SO_ITEM_ID").toString());
                map.put("FAK_REL_RELNO", hit.getSourceAsMap().get("FAK_REL_RELNO").toString());
                map.put("MEA_REL_TYPE", hit.getSourceAsMap().get("MEA_REL_TYPE").toString());
                map.put("FAD_REL_GOODSUNIT", hit.getSourceAsMap().get("FAD_REL_GOODSUNIT").toString());
                listMap.add(map);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 根据id  查询的 数据
        Map<String, Object> source = documentFields.getSource();

        // 组合参数值
        List<Map<String, Object>> tempList = new ArrayList<>();
        if (listMap.size() > 0) {
            listMap.forEach(params -> {
                Map<String, Object> map = new HashMap<>();
                map.putAll(params);
                map.putAll(source);
                tempList.add(map);
            });
        }
        return tempList;
    }


    /**
     *   嵌套文档 查询
     * @param indexName
     * @return
     */
    public JSONObject nestedList(String indexName) {
        try {
            //创建SearchRequest对象
            SearchRequest request = new SearchRequest(indexName);
            //创建复合查询对象
            BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
            //封装查询条件
            boolQuery.must(QueryBuilders.matchQuery("phones.phone", "19975262233"));
            boolQuery.must(QueryBuilders.matchQuery("phones.addr", "湖北宜昌"));
            //创建嵌套查询对象
            NestedQueryBuilder nestedQueryBuilder = QueryBuilders.nestedQuery("phones", boolQuery, ScoreMode.None);
            //创建并设置SearchSourceBuilder对象
            SearchSourceBuilder builder = new SearchSourceBuilder();
            builder.query(nestedQueryBuilder);
            // 将条件放入request中
            request.source(builder);
            //执行查询
            SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
            SearchHit[] hits = response.getHits().getHits();
            for (SearchHit hit : hits) {
                System.out.println(hit);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}

